package com.sec.android.app.myfiles.util;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.sec.android.app.myfiles.info.AppFeatures;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.R;

public final class VZCloudUtils {
    private static final String TAG = "VZCloudUtils";
    private static final String VZW_ClOUD_PKGNAME = "com.vcast.mediamanager";
    public static final String VZW_CLOUD_FILES = "com.vcast.mediamanager.ACTION_FILES";

    public static void prepareOptionsMenu(Context context, Menu menu, int menuitem, boolean canShowOptionsMenu) {
        MenuItem vzCloud = menu.findItem(menuitem);
        if (null == vzCloud) {
            return;
        }
        vzCloud.setVisible(false);
        if (!AppFeatures.IS_VZW || context == null || !isVZCloudEnabled(context) || !canShowOptionsMenu) {
            return;
        } 
        String appName = context.getString(R.string.verizon_cloud);
        if (!TextUtils.isEmpty(appName)) {
            vzCloud.setVisible(true);
            vzCloud.setTitle(appName);
        }
    }

    private static boolean isVZCloudEnabled(Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            if (pm != null) {
                PackageInfo pkginfo = pm.getPackageInfo(VZW_ClOUD_PKGNAME, PackageManager.GET_META_DATA);
                return (((pkginfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) && (pkginfo.applicationInfo.enabled));
            }
        } catch (NameNotFoundException e) {
            Log.e(TAG, "hasVZCloudpkg|Package not found");
        }
        return false;
    }

    public static void launchVZCloud(Context context, String action) {
        final Intent vzIntent = new Intent();
        vzIntent.setAction(action);
        try {
            context.startActivity(vzIntent);
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "launchVZCloud| intent not found");
        }
    }
}
